package com.metadata_system.system.response;

import com.metadata_system.common.db.entity.UserLoginLog;
import com.metadata_system.common.enums.AllConst;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.ComponentScan;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ComponentScan("com.metadata_system.common.db")
public class UserLoginLogResponse {
    private Integer id;
    private String username;
    private String deviceId;
    private Object role;
    private String ip;
    private Object status;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createdTime;

    public static UserLoginLogResponse formatData(UserLoginLog userLoginLog){
        return UserLoginLogResponse.builder()
                .id(userLoginLog.getId())
                .ip(userLoginLog.getIp())
                .role(AllConst.IdName(userLoginLog.getRole(),AllConst.UserRoleMap))
                .username(userLoginLog.getUsername())
                .deviceId(userLoginLog.getDeviceId())
                .status(AllConst.IdName(userLoginLog.getStatus(),AllConst.LoginStatusArr))
                .createdTime(userLoginLog.getCreatedTime())
                .build();
    }

    public static List<UserLoginLogResponse> formatData(List<UserLoginLog> userLoginLogList){
        List<UserLoginLogResponse> loginLogResponses=new ArrayList<>();
        for (UserLoginLog userLoginLog : userLoginLogList) {
            loginLogResponses.add(UserLoginLogResponse.formatData(userLoginLog));
        }
        return loginLogResponses;
    }
}
